package com.example.fairytset;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tx1;
    private TextView tx2;
    private TextView tx3;
    private  TextView tx4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        bindViews();
        tx1.performClick();
    }

    private void bindViews(){
        tx1 = findViewById(R.id.tx1);
        tx2 = findViewById(R.id.tx2);
        tx3 = findViewById(R.id.tx3);
        tx4 = findViewById(R.id.tx4);

        tx1.setOnClickListener(this);
        tx2.setOnClickListener(this);
        tx3.setOnClickListener(this);
        tx4.setOnClickListener(this);

    }

    private void setSelected(){
        tx1.setSelected(false);
        tx2.setSelected(false);
        tx3.setSelected(false);
        tx4.setSelected(false);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tx1:
                setSelected();
                tx1.setSelected(true);
                replacFragment(new firstFragment());
                break;
            case R.id.tx2:
                setSelected();
                tx2.setSelected(true);
                replacFragment(new PicFragment());
                break;
            case R.id.tx3:
                setSelected();
                tx3.setSelected(true);
                replacFragment(new talkFragment());
                break;
            case R.id.tx4:
                setSelected();
                tx4.setSelected(true);
                replacFragment(new MyFragment());
                break;

                default:
                    break;
        }

    }

    private void replacFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fra,fragment);
        transaction.commit();
    }
}
